package com.gitee.wsl.text.replacer

class ReplacerChain(vararg strReplacers: AbstractStrReplacer) : AbstractStrReplacer() {

    private val replacers: MutableList<AbstractStrReplacer> = mutableListOf()

    init {
        for (strReplacer in strReplacers) {
            addChain(strReplacer)
        }
    }

    operator fun iterator(): Iterator<AbstractStrReplacer> {
        return replacers.iterator()
    }

    fun addChain(element: AbstractStrReplacer): ReplacerChain {
        replacers.add(element)
        return this
    }

     override fun replace(str: CharSequence, pos: Int, out: Appendable): Int {
        var consumed = 0
        for (strReplacer in replacers) {
            consumed = strReplacer.replace(str, pos, out)
            if (0 != consumed) {
                return consumed
            }
        }
        return 0
    }

}